package com.example.demo.util;

import com.example.demo.core.entity.Menu;
import com.example.demo.core.entity.Permission;

import java.util.*;

public class PermissionUtil {
    public static Collection<Permission> TreeToList(Collection<Permission> permissions){
        // 创建permission list 存储权限列表
        List<Permission> permissionList = new ArrayList<>();

        // 添加权限(order 0)
        List<Permission> p0List = new ArrayList<>(permissions);
        for(Permission p0 : p0List){
            permissionList.add(p0);

            // 添加权限(order 1)
            List<Permission> p1List = new ArrayList<>(p0.getChildren());
            for(Permission p1 : p0.getChildren()){
                permissionList.add(p1);

                // 添加权限(order 2)
                for(Permission p2 : p1.getChildren()){
                    permissionList.add(p2);
                }
            }
        }

        return permissionList;
    }

    public static Collection<Permission> ListToTree(Collection<Permission> permissions){
        // 三级嵌套权限树
        // Order 0权限
        List<Permission> p0List = new ArrayList<>();
        for(Permission p : permissions){
            if(p.getOrder() == 0) {
//                p0List.add(p);
                Permission tmp = new Permission(p);
                p0List.add(tmp);
            }
        }
        Collections.sort(p0List, (Permission arg0,Permission arg1) -> {
            int PermissionId0 = arg0.getId();
            int PermissionId1 = arg1.getId();
            if ( PermissionId1 < PermissionId0) {
                return 1;
            } else if (PermissionId0 == PermissionId1) {
                return 0;
            } else {
                return -1;
            }
        });
        for(Permission p0 : p0List) {
            // Order 1权限
            //System.out.println(p0.getPath());
            String p0_prefix = p0.getPath();
            Set<Permission> p1Set = new TreeSet<>();
            for(Permission p : permissions)
                if(p.getOrder()==1 && p.getPath().startsWith(p0_prefix)) {

//                    p1Set.add(p);
                    Permission tmp = new Permission(p);
                    p1Set.add(tmp);
                }
            p0.setChildren(p1Set);
            for(Permission p1 : p1Set){
                // Order 2权限
                //System.out.println(p1.getPath());
                String p1_prefix = p1.getPath();
                Set<Permission> p2Set = new TreeSet<>();
                for(Permission p : permissions)
                    if(p.getOrder()==2 && p.getPath().startsWith(p1_prefix)) {
//                        p2Set.add(p);
                        Permission tmp = new Permission(p);
                        p2Set.add(tmp);
                    }
                p1.setChildren(p2Set);
//                for (Permission p2 : p2Set){
//                    System.out.println(p2.getPath());
//                }
            }
        }
        return p0List;
    }

    /**
    * @Description: 将菜单栏转化为树形结构
    * @Param:  menus: 菜单列表
    * @return:  树形菜单栏
    * @Author: wangze
    */
    public static Collection<Menu> MenuListToTree(Collection<Menu> menus){
        List<Menu> p0List = new ArrayList<>();
        for(Menu p : menus){
            if(p.getOrder() == 0)
                p0List.add(p);
        }
        Collections.sort(p0List, (Menu arg0,Menu arg1) -> {
            int MenuId0 = arg0.getId();
            int MenuId1 = arg1.getId();
            if ( MenuId1 < MenuId0) {
                return 1;
            } else if (MenuId0 == MenuId1) {
                return 0;
            } else {
                return -1;
            }
        });
        for (Menu p0 : p0List){
            Integer menuId = p0.getId();
            Set<Menu> sortSet = new TreeSet();
//                    ((Menu o1, Menu o2) -> {
//                int MenuId1 = o1.getId();
//                int MenuId2 = o1.getId();
//                if (MenuId1 > MenuId2) {
//                    return 1;
//                } else if (MenuId1 == MenuId2) {
//                    return 0;
//                } else {
//                    return -1;
//                }
//            });
            for(Menu p : menus){
                if(p.getOrder()==1 && p.getPid()==menuId)
                    sortSet.add(p);
            }
            p0.setChildren(sortSet);
        }
        return p0List;
    }
}
